package club.zhilin.guli.service.edu.controller.backend;


import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.service.edu.domain.Teacher;
import club.zhilin.guli.service.edu.domain.qo.TeacherQueryObject;
import club.zhilin.guli.service.edu.service.ITeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zhilin
 * @since 2022-01-11 15:29:01
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TeacherController {

    private final ITeacherService iTeacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/list")
    public CommonResult<List<Teacher>> getAll() {
        return CommonResult.success(iTeacherService.list());
    }

    @ApiOperation(value = "根据id删除讲师", notes = "根据id删除讲师,逻辑删除")
    @DeleteMapping("/remove/{id}")
    public CommonResult<String> removeTeacherById(@ApiParam(value = "讲师id", type = "path") @PathVariable String id) {
        // 删除讲师头像
        iTeacherService.removeAvatarById(id);
        boolean result = iTeacherService.removeById(id);
        return result ? CommonResult.success("删除成功") : CommonResult.failed("需要删除的数据不存在,或已删除");
    }

    @ApiOperation(value = "讲师分页列表查询")
    @GetMapping("/list/{page}/{limit}")
    public CommonResult<Map<String, Object>> limitTeacherList(@ApiParam(value = "页码", required = true, example = "1") @PathVariable Long page, @ApiParam(value = "显示数据条数", required = true, example = "5") @PathVariable Long limit, @ApiParam("讲师查询对象") TeacherQueryObject teacherQueryObject) {
        return iTeacherService.selectList(page, limit, teacherQueryObject);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("/teacher")
    public CommonResult<String> saveTeacher(@ApiParam("讲师对象") @RequestBody Teacher teacher) {
        boolean result = iTeacherService.save(teacher);
        return result ? CommonResult.success("保存成功") : CommonResult.failed("保存失败");
    }

    @ApiOperation(value = "更新讲师信息")
    @PutMapping("/teacher")
    public CommonResult<String> updateTeacher(@ApiParam("讲师对象") @RequestBody Teacher teacher) {
        boolean result = iTeacherService.updateById(teacher);
        return result ? CommonResult.success("更新成功") : CommonResult.failed("更新失败");
    }

    @ApiOperation(value = "根据id查询讲师对象")
    @GetMapping("/teacher/{id}")
    public CommonResult<Teacher> getTeacher(@ApiParam("讲师id") @PathVariable Long id) {
        Teacher teacher = iTeacherService.getById(id);
        return teacher != null ? CommonResult.success(teacher) : CommonResult.failed("查询的数据不存在");
    }

    @ApiOperation(value = "根据讲师id列表批量删除讲师")
    @DeleteMapping("/batchRemoveTeacher")
    public CommonResult<String> removeTeachers(@ApiParam(value = "需要删除的讲师id列表", required = true) @RequestBody List<String> ids) {
        boolean result = iTeacherService.removeByIds(ids);
        return result ? CommonResult.success("批量删除成功") : CommonResult.failed("批量删除失败");
    }

    @ApiOperation(value = "根据关键字查询讲师名列表")
    @GetMapping("/list/name/{key}")
    public CommonResult<List<Map<String, Object>>> selectName(@ApiParam(value = "关键字", required = true) @PathVariable String key) {
        return CommonResult.success(iTeacherService.accordingKeySearchNameList(key));
    }
}

